/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-2
 * @Time  : 下午8:11
 * @brief :
 */
exports.code =function(id){
    var $widget = $('.widget-code-'+id),
        $messageBtn = $widget.find('.btn-message'),
        $voice = $widget.find('.voice');

    function sendCode(type,callback){
        $.post('/account/sendmyidentitycode',{
            'type':type
        },function(data) {
            callback();
        },'json');
    }


    function intervalCount(){
        var count = 60,
            countInterval = setInterval(function(){
            $messageBtn.text(count+'秒后重新获取');
            if(count>0){
                count--;
            }else{
                count = 60;
                clearInterval(countInterval);
                $voice.slideDown();
                $messageBtn.removeClass('btn-disable').addClass('btn-blank1').text('短信获取');
            }
        },1000);
    }


    $messageBtn.click(function(){
        var me = this;
        if($(this).hasClass('btn-blank1')){
            $(this).removeClass('btn-blank1').addClass('btn-disable');
            $voice.hide();
            sendCode(0,function(){
                intervalCount();
//                var count = 60,
//                    countInterval = setInterval(function(){
//                        $messageBtn.text(count+'秒后重新获取');
//                        if(count>0){
//                            count--;
//                        }else{
//                            count = 60;
//                            clearInterval(countInterval);
//                            $voice.slideDown();
//                            $(me).removeClass('btn-disable').addClass('btn-blank1').text('短信获取');
//                        }
//                    },1000);
            });
        }
    });

    $voice.find('.btn-voice').click(function(e){
        e.stopPropagation();
        $messageBtn.removeClass('btn-blank1').addClass('btn-disable');
        $voice.hide();
        sendCode(1,function(){
            intervalCount();
        });
    });

};